package com.btpj.module_main.ui.mine

import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import com.btpj.jetpack.base.BaseFragment
import com.btpj.jetpack.arouter.login.LoginServiceImpWrap
import com.btpj.module_main.R
import com.btpj.module_main.databinding.FragmentMineBinding
import kotlinx.android.synthetic.main.fragment_mine.*

/**
 * 我的
 *
 * @author LTP 2020/7/24
 */
class MineFragment : BaseFragment<MineViewModel, FragmentMineBinding>() {

    companion object {

        @JvmStatic
        fun newInstance() = MineFragment()
    }

    override fun getLayoutId() = R.layout.fragment_mine

    override fun initView() {
        mBinding.user = LoginServiceImpWrap.getUserInfo()

        tv_name.setOnClickListener {
            if (!LoginServiceImpWrap.isLogin()) {
                LoginServiceImpWrap.start(requireContext())
            }
        }
        iv_exit.setOnClickListener {
            AlertDialog.Builder((requireContext())).setTitle("提示")
                .setMessage("确定退出吗？")
                .setPositiveButton(
                    "确定"
                ) { dialog, _ ->
                    dialog.dismiss()
                    LoginServiceImpWrap.removeUserInfo()
                }.setNegativeButton(
                    "取消"
                ) { dialog, _ ->
                    dialog.dismiss()
                }.show()
        }
    }

    override fun createObserve() {
        super.createObserve()
        LoginServiceImpWrap.getLiveData().observe(this, Observer {
            mBinding.user = it
        })
    }
}